package com.srs.backend.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.srs.backend.mapper.ClientMapper;
import com.srs.backend.mapper.FoodMaterialMapper;
import com.srs.backend.pojo.*;
import com.srs.backend.service.ClientService;
import com.srs.backend.utils.Result;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ClientServicelmpl implements ClientService {
        @Resource
        private ClientMapper clientMapper;

        @Override
        public Result add(Map<String, String> data) {
                String name = data.get("name");
                String sex = data.get("sex");
                String phone = data.get("phone");
                String preferStr = data.get("prefer");
                Integer prefer = null;
                String birthdayStr = data.get("birthday");
                Date birthday = null;
                String remark = data.get("remark");

                Integer uid;

                try {
                        uid = Integer.parseInt(data.get("uid"));
                } catch (Exception e) {
                        return Result.error("uid有误");
                }

                try {
                        prefer = Integer.parseInt(preferStr);
                } catch (NumberFormatException e) {
                        return Result.error("喜爱食物类型错误");
                }

                if (name == null || name.length() == 0) {
                        return Result.error("用户名称不能为空");
                }

                if (name.length() > 20) {
                        return Result.error("用户名称过长");
                }

                if (phone.length() != 11) {
                        return Result.error("手机号码长度错误");
                }

                try {
                        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
                        birthday = ft.parse(birthdayStr);
                } catch (ParseException e) {
                        return Result.error("生日格式有误");
                }

                clientMapper.insert(new Client(null, name, sex, phone, birthday,remark,prefer, uid));

                return Result.ok("success");

        }

        @Override
        public Result delete(Map<String, String> data) {
                String idStr = data.get("id");
                Integer id = null;
                try {
                        id = Integer.parseInt(idStr);
                } catch (NumberFormatException e) {
                        return Result.error("用户id数据有误");
                }

                Client client = clientMapper.selectById(id);
                if (client == null) {
                        return Result.error("当前用户不存在或已删除");
                }
                clientMapper.deleteById(client);
                return Result.ok();
        }

        @Override
        public Result modify(Map<String, String> data) {
                String idStr = data.get("id");
                Integer id = null;
                try {
                        id = Integer.parseInt(idStr);
                } catch (NumberFormatException e) {
                        return Result.error("用户id数据有误");
                }

                Client client = clientMapper.selectById(id);
                if (client == null) {
                        return Result.error("当前用户不存在或已删除");
                }

                String name = data.get("name");
                String sex = data.get("sex");
                String phone = data.get("phone");
                String preferStr = data.get("prefer");
                Integer prefer = null;
                String birthdayStr = data.get("birthday");
                Date birthday = null;
                String remark = data.get("remark");

                try {
                        prefer = Integer.parseInt(preferStr);
                } catch (NumberFormatException e) {
                        return Result.error("喜爱食物类型错误");
                }

                if (name == null || name.length() == 0) {
                        return Result.error("用户名称不能为空");
                }

                if (name.length() > 20) {
                        return Result.error("用户名称过长");
                }

                if (phone.length() != 11) {
                        return Result.error("手机号码长度错误");
                }

                try {
                        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
                        birthday = ft.parse(birthdayStr);
                } catch (ParseException e) {
                        return Result.error("生日格式有误");
                }

                client.setBirthday(birthday);
                client.setName(name);
                client.setPhone(phone);
                client.setPrefer(prefer);
                client.setSex(sex);
                client.setRemark(remark);

                clientMapper.updateById(client);
                return Result.ok();
        }

        @Override
        public Result getAll() {
                List<Client> clients = clientMapper.selectList(null);
                return Result.ok(clients);
        }

        @Override
        public Result getClientInfo(Map<String, String> data) {
                String uidStr = data.get("uid");
                Integer uid;
                try {
                        uid = Integer.parseInt(uidStr);
                } catch (Exception e) {
                        return  Result.error("uid类型有误");
                }
                QueryWrapper<Client> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("uid", uid);
                Client client = clientMapper.selectOne(queryWrapper);
                if (client == null) {
                        return Result.error("该 顾客 不存在或已被删除");
                }
                return Result.ok(client);
        }
}
